﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BaseTaskHelper.cs" company="DHGMS Solutions">
//   Copyright 2004-2014 DHGMS Solutions.
//      
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//      
//   http://www.apache.org/licenses/LICENSE-2.0
//      
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// <summary>
//   JobBase class for a task
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.TaskHelper
{
    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;

    using Dhgms.TplHelper.Model.Info;

    /// <summary>
    /// JobBase class for a task
    /// </summary>
    /// <typeparam name="TTaskResultClass">
    /// The type of result being returned by the task
    /// </typeparam>
    public abstract class BaseTaskHelper<TTaskResultClass> : NonGenericBaseTaskHelper
        where TTaskResultClass : TaskResultBase, new()
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseTaskHelper{TTaskResultClass}"/> class. 
        /// </summary>
        /// <param name="name">
        /// The task Name.
        /// </param>
        protected BaseTaskHelper(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            this.Name = name;
        }

        /// <summary>
        /// Gets the name of the current task
        /// </summary>
        protected string Name { get; private set; }

        /// <summary>Entry point for doing the donkey work.</summary>
        /// <returns>The Execution Result.</returns>
        public abstract Task<TTaskResultClass> GetTaskResult();

        /// <summary>
        /// Runs a task instantly
        /// </summary>
        /// <returns>
        /// Job Result Information
        /// </returns>
        public TTaskResultClass RunSynchronously()
        {

            if (Debugger.IsAttached)
            {
                return GetTaskResult().Result;
            }

            var t = this.GetTaskResult();
            t.RunSynchronously();

            if (!t.IsFaulted)
            {
                return t.Result;
            }

            if (t.Exception == null)
            {
                throw new TaskCanceledException();
            }

            // don't pass back an aggregate exception if we don't need to
            if (t.Exception.InnerExceptions != null && t.Exception.InnerExceptions.Count == 1)
            {
                throw t.Exception.InnerExceptions[0];
            }

            throw t.Exception;
        }
    }
}